package stardust.core.io;


import java.io.IOException;
import java.io.InputStream;
import stardust.core.io.IOUtils;


public class InputReader extends InputStream
{

  private InputStream inputStream;
  
  public InputReader(InputStream inputStream)
  {
    this.inputStream = inputStream;
  }

  public int read() throws IOException
  {
    return this.inputStream.read();
  }
  
  public int read(byte[] data) throws IOException
  {
    return this.inputStream.read(data);
  }

  public int read(byte[] data, int offset, int len) throws IOException
  {
    return this.inputStream.read(data, offset, len);
  }

  public byte[] requiredRead(int len) throws IOException
  {
    return IOUtils.read(this.inputStream, len);
  }

  public Character readChar() throws IOException
  {
    byte b0;
    byte b1;

    b0 = (byte) this.inputStream.read();

    if (b0 == -1)
    {
      return null;
    }

    if (b0 < 0 && (b1 = (byte) this.inputStream.read()) != -1)
    {
      return new String(new byte[] {b0, b1}).charAt(0);
    }
    else
    {
      return (char) b0;
    }
  }

  public String readLine() throws IOException
  {
    StringBuffer buffer = new StringBuffer();
    Character c;

    while ((c = this.readChar()) != null && c != '\n')
    {
      if (c != '\r')
      {
        buffer.append(c);
      }
    }

    return c != null || (c == null && buffer.length() > 0) ? buffer.toString() : null;
  }
  
  public void close() throws IOException
  {
    this.inputStream.close();
  }
  
}
